package com.camunda.workflow.domain;

import com.camunda.workflow.domain.entity.*;
import org.camunda.bpm.engine.impl.persistence.entity.*;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper
public interface MapStructMapper {
    MapStructMapper INSTANCE = Mappers.getMapper(MapStructMapper.class);

    @Mapping(target = "id", source = "id")
    UserEntity toCamundaUser(SysUser entity);
    List<UserEntity> toCamundaUserList(List<SysUser> entityList);
    SysUser toSysUser(UserEntity dto);
    List<SysUser> toSysUserList(List<UserEntity> dtoList);

    // GroupEntity == SysRole
    GroupEntity toCamundaGroup(SysRole entity);
    List<GroupEntity> toCamundaGroupList(List<SysRole> entityList);
    SysRole toSysRole(GroupEntity dto);
    List<SysRole> toSysRoleList(List<GroupEntity> dtoList);

    //
    TenantEntity toCamundaTenant(SysTenant entity);
    List<TenantEntity> toCamundaTenantList(List<SysTenant> entityList);
    SysTenant toSysTenant(TenantEntity dto);
    List<SysTenant> toSysTenantList(List<TenantEntity> dtoList);


    // MembershipEntity == SysUserRole
    @Mapping(target = "group", source = "role")
    MembershipEntity toCamundaMembership(SysUserRole entity);
    List<MembershipEntity> toCamundaMembershipList(List<SysUserRole> entityList);
    @Mapping(target = "role", source = "group")
    SysUserRole toSysUserRole(MembershipEntity dto);
    List<SysUserRole> toSysUserRoleList(List<MembershipEntity> dtoList);

    //TenantMembershipEntity == SysTenantUserRole
    @Mapping(target = "group", source = "role")
    TenantMembershipEntity toCamundaTenantMembership(SysTenantUserRole entity);
    List<TenantMembershipEntity> toCamundaTenantMembershipList(List<SysTenantUserRole> entityList);
    @Mapping(target = "role", source = "group")
    SysTenantUserRole toSysTenantUserRole(TenantMembershipEntity dto);
    List<SysTenantUserRole> toSysTenantUserRole(List<TenantMembershipEntity> dtoList);

    /**
     * 自定义转换器
     */
    default Long stringToLong(String value) {
        if (value == null || value.isEmpty()) {
            return null;
        }
        try {
            return Long.parseLong(value);
        } catch (NumberFormatException e) {
            // 处理非整数型字符串
            return null; // 或者返回一个默认值，如 0L
        }
    }
}
